package org.cloudnote.service.impl;

import org.apache.ibatis.annotations.Param;
import org.cloudnote.bean.Note;
import org.cloudnote.bean.NoteBook;
import org.cloudnote.bean.Share;
import org.cloudnote.mapper.NoteBookMapper;
import org.cloudnote.mapper.NoteMapper;
import org.cloudnote.mapper.ShareMapper;
import org.cloudnote.service.NoteBookService;
import org.cloudnote.service.NoteService;
import org.cloudnote.util.NoteUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NoteServiceImpl implements NoteService {

    @Autowired
    private NoteMapper noteMapper;
    @Autowired
    private ShareMapper shareMapper;

    @Override
    public List<Note> getByBookId(String noteBookId) {
        List<Note> notes = noteMapper.getByBookId(noteBookId);
        return notes;
    }

    @Override
    public Note getByNoteId(String noteId) {
        Note note = noteMapper.getByNoteId(noteId);
        return note;
    }

    @Override
    public void save(Note note) {
        note.setNoteId(NoteUtil.createId());
        note.setNoteBookId(note.getNoteBookId());
        note.setUserId(note.getUserId());
        note.setNoteStatusId("1");
        note.setNoteTypeId("1");
        note.setNoteTittle(note.getNoteTittle());
        note.setNoteBody("");
        long time = System.currentTimeMillis();
        note.setNoteCreateTime(time);
        note.setNoteLastModifiedTime(time);
        System.out.println(note.getNoteCreateTime());
        System.out.println(note.getNoteLastModifiedTime());
        noteMapper.save(note);
    }

    @Override
    public void update(Note note) {
        noteMapper.update(note);
    }

    @Override
    public void recycle(String noteId) {

        noteMapper.recycle(noteId);
    }

    @Override
    public Share getShareByNoteId(String noteId) {
        Share share = shareMapper.getShareByNoteId(noteId);
        return share;
    }

    @Override
    public void addShare(Share share) {
        shareMapper.addShare(share);
    }

    @Override
    public List<Share> getByTittle(String noteTittle) {
        String tittle="";
        if(!"".equals(noteTittle)&&noteTittle!=null){
            tittle="%"+noteTittle+"%";
        }
        else {
            tittle="%";
        }
        List<Share> notes = shareMapper.getByTittle(tittle);
        return notes;
    }

    @Override
    public Share getById(String shareId) {
        Share share = shareMapper.getById(shareId);
        return share;
    }

    @Override
    public int moveNote(String bookId, String noteId) {
        int count = noteMapper.moveNote(bookId, noteId);
        return count;
    }
}
